/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";
$("#startTime").val(_start);
$("#endTime").val(_end);
laydate.render({
    elem: '#startTime',
    type: 'datetime'

});
laydate.render({
    elem: '#endTime'
    , type: 'datetime'
});


$('#dataTable').bootstrapTable({
    url: '/WopayMaintainController/queryWopayMainTainDetail',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: true,
    pageList: [30, 50, 100],
    clickToSelect: true,
    singleSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    showFooter: true,
    responseHandler: function (res) {
        return {
            "total": res.total,
            "rows": res.resultList
        };
    },
    columns: [
        {
            checkbox: true
        },
        {
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {align: 'center', field: 'id', visible: false},
        {
            align: 'center', field: 'bankName', title: '银行名称'
        },
        {
            align: 'center', field: 'startTime', title: '维护开始时间',
            formatter: function (v, row, index) {
                if (v != null && v != "") {
                    return format(v);
                } else {
                    return "-"
                }
            }
        },
        {
            align: 'center', field: 'endTime', title: '维护结束时间',
            formatter: function (v, row, index) {
                if (v != null && v != "") {
                    return format(v);
                } else {
                    return "-"
                }
            }
        },
        {
            align: 'center', field: 'adminName', title: '操作人'
        },
        {
            align: 'center', field: 'remark', title: '备注'
        }, {
            align: 'center',
            title: '操作',
            formatter: function (v, row, index) {
                if (row.dept === undefined || row.dept === null || row.dept === '') {
                    return '-';
                }

                if (row.dept=== 'caiwu'){
                    return "<div shiro:hasAnyRoles='caiwu'><a href='javascript:void(0)'  onclick='toMaintain(" + JSON.stringify(row) + ")' > 修改 </a> &nbsp; " +
                        "<a href='javascript:void(0)' onclick='toDelete(" + JSON.stringify(row) + ")' > 删除 </a></div>";
                }
                return '-';
            }
        }
    ]
});


$("#search").click(function () {
    reloadData();
});

//重载数据
function reloadData() {
    $("#dataTable").bootstrapTable("refresh");
}

function toMaintain(v) {
    $("#maintBankId").val(v.id)
    $("#maintBankName").val(v.bankName)
    if (v.startTime != null && v.startTime != "") {
        $("#startTime").val(format(v.startTime))
    } else {
        $("#startTime").val("")
    }
    if (v.endTime != null && v.endTime != "") {
        $("#endTime").val(format(v.endTime))
    } else {
        $("#endTime").val("")
    }
    $("#myModal1").modal("show");
}

$("#toSave").click(function () {
    if (($("#startTime").val() == null || $("#startTime").val() == '') ||
        ($("#endTime").val() == null || $("#endTime").val() == '' )) {
        layer.msg("请选择维护时间");
    }
    var _start = new Date($("#startTime").val()).getTime();
    var _end = new Date($("#endTime").val()).getTime();
    if (_end <= _start) {
        layer.msg("开始时间不能大于结束时间！");
        return;
    }
    $.post("/WopayMaintainController/updateWopayMaintain", $("#maintainForm").serialize(), function (data) {
        layer.msg(data.message);
        if (data.success) {
            reloadData()
            $("#myModal1").modal("hide");
        }
    })
})

function toDelete(v) {
    var id = v.id
    $.confirm({
        title: '确认!',
        content: '确定删除维护时间吗？',
        confirm: function () {
            $.post("/WopayMaintainController/updateWopayMaintain?id="
                + id, function (data) {
                layer.msg(data.message);
                if (data.success) {
                    reloadData()
                    $("#myModal1").modal("hide");
                }
            });
        },
        cancel: function () {

        }
    });
}
function queryParams(params) {
    return {
        size: params.pageSize,
        page: params.pageNumber,
        bankName: $.trim($("#bankName").val())
    };
}
